﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class PCDienThoaiDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_PCDienThoai> GetAllPCDienThoai()
        {
            return DBDanhMuc.dm_PCDienThoais.Where(p => p.IsDelete == false).ToList();
        }
        public dm_PCDienThoai AddNewPCDienThoai(dm_PCDienThoai PCDienThoai)
        {
            DBDanhMuc.dm_PCDienThoais.InsertOnSubmit(PCDienThoai);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeletePCDienThoai(int PCDienThoaiID)
        {
            dm_PCDienThoai PCDienThoaiNew =
                DBDanhMuc.dm_PCDienThoais.Single(p => p.Id == PCDienThoaiID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_PCDienThoais.DeleteOnSubmit(PCDienThoaiNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeletePCDienThoai(dm_PCDienThoai PCDienThoai)
        {
            dm_PCDienThoai PCDienThoaiDelete =
                DBDanhMuc.dm_PCDienThoais.Single(p => p.Id == PCDienThoai.Id);
            if (PCDienThoaiDelete.hs_NhanVien_PCDienThoais.Count() == 0)
            {
                PCDienThoaiDelete.Id = PCDienThoai.Id;
                PCDienThoaiDelete.UpdateDate = DateTime.Now;
                PCDienThoaiDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                PCDienThoaiDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdatePCDienThoai(dm_PCDienThoai PCDienThoai)
        {
            dm_PCDienThoai PCDienThoaiUpdate =
                DBDanhMuc.dm_PCDienThoais.Single(p => p.Id == PCDienThoai.Id);

            PCDienThoaiUpdate.Id = PCDienThoai.Id;
            PCDienThoaiUpdate.TienPCDienThoai = PCDienThoai.TienPCDienThoai;
            PCDienThoaiUpdate.UpdateDate = PCDienThoaiUpdate.UpdateDate;
            PCDienThoaiUpdate.UpdateBy = PCDienThoaiUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_PCDienThoai> SearchPCDienThoaiPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_PCDienThoai>)DBDanhMuc.dm_PCDienThoais.Where(p => p.TienPCDienThoai == decimal.Parse(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_PCDienThoai GetPCDienThoaiById(string id)
        {
            try
            {
                dm_PCDienThoai item = this.DBDanhMuc.dm_PCDienThoais.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_PCDienThoai GetPCDienThoaiByTenCheck(decimal ten)
        {

            IEnumerable<dm_PCDienThoai> list = this.DBDanhMuc.dm_PCDienThoais.Where(p => p.TienPCDienThoai == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_PCDienThoai GetPCDienThoaiByTen(decimal ten)
        {

            IEnumerable<dm_PCDienThoai> list = this.DBDanhMuc.dm_PCDienThoais.Where(p => p.TienPCDienThoai == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_PCDienThoai> RecycleGetPCDienThoaiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_PCDienThoais.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_PCDienThoai> GetPCDienThoaiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_PCDienThoais.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_PCDienThoai BuildNewPCDienThoai(PCDienThoaiForm form)
        {
            dm_PCDienThoai newPCDienThoai = new dm_PCDienThoai();
            if(!string.IsNullOrEmpty(form.TienPCDienThoai))
                newPCDienThoai.TienPCDienThoai =decimal.Parse(form.TienPCDienThoai.Replace(".",""),null);
            newPCDienThoai.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newPCDienThoai.CreateDate = DateTime.Now;
            newPCDienThoai.IsDelete = false;
            return newPCDienThoai;
        }
        public int PhucHoiPCDienThoai(int id)
        {
            dm_PCDienThoai phuchoi = new dm_PCDienThoai();
            phuchoi = this.GetPCDienThoaiById(Convert.ToString(id));
            dm_PCDienThoai item = new dm_PCDienThoai();
            item = this.GetPCDienThoaiByTenCheck(phuchoi.TienPCDienThoai.Value);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_PCDienThoai BuildEditPCDienThoai(PCDienThoaiForm form)
        {
            dm_PCDienThoai newPCDienThoai = new dm_PCDienThoai();
            newPCDienThoai.Id = form.id;
            if (!string.IsNullOrEmpty(form.TienPCDienThoai))
                newPCDienThoai.TienPCDienThoai = decimal.Parse(form.TienPCDienThoai.Replace(".", ""), null);
            newPCDienThoai.UpdateDate = DateTime.Now;
            newPCDienThoai.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newPCDienThoai;
        }
        public dm_PCDienThoai GetLoaiCongChucIdMax()
        {
            IList<dm_PCDienThoai> list = DBDanhMuc.dm_PCDienThoais.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public PCDienThoaiForm ConvertToForm(dm_PCDienThoai obj)
        {
            PCDienThoaiForm form = new PCDienThoaiForm();
            form.id = obj.Id;
            form.TienPCDienThoai = obj.TienPCDienThoai!=null?obj.TienPCDienThoai.Value.ToString("#,#").Replace(",","."):"";
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}